import React, { Component } from 'react'
import './Header.css'
import { nanoid } from 'nanoid'
import PropTypes from 'prop-types'


export default class Header extends Component {
  // 类型限制
  static propTypes = {
    add: PropTypes.func.isRequired
  }

  render() {
    return (
      <div className="todo-header">
        <input type="text" onKeyUp={this.handleKeyUp} placeholder="请输入你的任务名称，按回车键确认" />
      </div>
    )
  }
  // 键盘回车事件    
  handleKeyUp = (e) => {
    let { keyCode, target } = e
    if (keyCode !== 13) return
    this.props.add({ id: nanoid(), name: target.value, done: false })

    //清空输入
    target.value = ''
    // console.log(target.value);
  }
}
